/**
 * Created by huangzy on 2015/5/22.
 * http://wx.12301.cc/tpl/membercard/js/membercard.memmsg.js
 */
var S = RichBase.extend({
	statics : {
		wuInitVal : "请连接读卡设备，后点击读取物理卡号",
		editMode : false,
		api : "call/jh_card.php"
	},
	EVENTS : {
		click : {
			".limitFormBox input:radio" : "onLimitInpClick",
			".limitFormBox .group .t" : "onLimitInpClick",
			"#saveBtn" : "onSubmit",
			"#readwuKa" : "readwuKa"
		},
		"blur" : {
			".limitFormBox .formInp" : "onLimitInpBlur",
			".requireFormBox .valifyInp" : "onValifyInpBlur"
		},
		"focus" : {
			".requireFormBox .valifyInp" : "onValifyInpFocus"
		}
	},
	readwuKa : function(that,e){
		var helloBossma = that.helloBossma;
		if(!helloBossma){
			alert("请使用IE浏览器读物理卡号");
			return false;
		}
		if(typeof helloBossma.open!="number" && typeof helloBossma.ICReaderRequest!="string"){
			alert("请使用IE浏览器并确认浏览器已安装GuoHe_ICReader_ActiveX插件");
			return false;
		}
		helloBossma.open();
		var val = helloBossma.ICReaderRequest();
		$("#phy_no_inp").val(val);
	},
	init : function(opt){
		var container = this.container = opt.container;
		var hidInps = this.hidInps =  container.find(".limitFormBox .hidInp");
		this.loadingMasker = $("#formLoading");
		this.helloBossma = document.getElementById("helloBossma");
		if(!$("#phy_no_inp").val()) $("#phy_no_inp").val(this.statics.wuInitVal);

		// false:编辑模式  true:新开卡模式
		var editMode = this.statics.editMode = $("#formMode").val()==0 ? false : true;
		var noticeType = $("#varNoticeTypeHidInp").val();
		container.find(".noticeTypeRadio[value="+noticeType+"]").trigger("click");
		hidInps.each(function(){
			var inp = $(this);
			var p = inp.parent();
			var val = inp.val();
			if(val==-1){
				p.find("label.nolimit input").trigger("click");
			}else{
				p.find("label input:radio").trigger("click").val(val);
			}
		})
	},
	onValifyInpBlur : function(that,e){
		var tarInp = $(e.currentTarget);
		var require = tarInp.attr("require");
		var val = tarInp.val();
		var errorTip = tarInp.parents(".line").find(".errortip");
		if(typeof require=="undefined") return false;
		if(tarInp.hasClass("repassword_inp")){ //确认密码
			var password_inp = $("#password_inp");
			var password = password_inp.val();
			if(password!==val){
				errorTip.show();
				tarInp.addClass("error");
			}
		}else if(tarInp.hasClass("password_inp")){ //6位数密码
			if(!val || val.length!=6){
				tarInp.addClass("error");
			}
		}else if(tarInp.hasClass("phy_no_inp")){//物理卡号

		}else{ //其它
			if(!val){
				tarInp.addClass("error");
				errorTip.show();
			}
		}

	},
	onValifyInpFocus : function(that,e){
		var tarInp = $(e.currentTarget);
		var val = tarInp.val();
		var errorTip = tarInp.parents(".line").find(".errortip");
		tarInp.removeClass("error");
		errorTip.hide();
		if(tarInp.hasClass("password_inp")) errorTip.show();
		if(val==that.statics.wuInitVal) tarInp.val("")
	},
	onLimitInpClick : function(that,e){
		var currentTarget = $(e.currentTarget);
		var label = currentTarget.parent();
		var hidInp = label.parent().children(".hidInp");
		var limitInp = label.children(".formInp");
		var limitVal = limitInp.val();
		var limitOrgiinVal = limitInp.attr("data-orginval");
		if(label.hasClass("nolimit")){
			hidInp.val("-1");
			label.next().children(".formInp").addClass("readonly").attr("readonly","readonly");
		}else{
			limitInp.removeClass("readonly").removeAttr("readonly");
			if(PFT_GLOBAL.U.isPositiveNum(limitVal)){ //如果是正整数
				hidInp.val(limitVal);
			}
		}
	},
	onLimitInpBlur : function(that,e){
		var tarInp = $(e.currentTarget);
		var val = tarInp.val();
		var limitOrgiinVal = tarInp.attr("data-orginval");
		var hidInp = tarInp.parents(".line").children(".hidInp");
		if(!PFT_GLOBAL.U.isPositiveNum(val)){ //如果不是正整数
			tarInp.val(limitOrgiinVal);
		}
		hidInp.val(tarInp.val());
	},
	onSubmit : function(that,e){
		e.preventDefault();
		var allow = true;
		var saveBtn = $("#saveBtn");
		$("#requireFormBox").find(".valifyInp").each(function(){
			var tarInp = $(this);
			tarInp.blur();
			if(tarInp.hasClass("error")){
				allow = false;
				return false;
			}
		})
		if(!allow) return false;
		var data = $("#memMsgForm").serialize();
		PFT_GLOBAL.G.Ajax({
			url : that.statics.api,
			type : "post",
			data : data,
			loading : function(){ saveBtn.addClass("loading")},
			removeLoading : function(){ saveBtn.removeClass("loading")},
			timeout : function(){
				console.log("保存数据服务器超时")
			},
			serveerError : function(){
				console.log("保存数据服务器出错")
			}
		},function(res){
			if(res.status=="success"){
				PFT_GLOBAL.U.Alert("success",'<p style="width:150px">保存成功</p>');
                window.location.href = "http://www.12301.cc/mcard_list.html";
			}else{
				PFT_GLOBAL.U.Alert("fail",'<p style="width:180px">'+res.msg+'</p>');
			}
		})
	}
});

new S({container:$("#formContainer")});